package com.caiyi.financial.nirvana.sms.mapper;

import com.caiyi.financial.nirvana.sms.base.mapper.BaseMapper;
import com.caiyi.financial.nirvana.sms.entity.SmsUserTemplate;
import com.caiyi.financial.nirvana.sms.querybean.UserTemplateQueryBean;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;
import java.util.Map;

@Mapper
public interface SmsUserTemplateMapper extends BaseMapper<SmsUserTemplate> {

    @Select("select s.template from sms_user_template s where s.account = #{account} ")
    List<String> selectTemplateListByAccount(@Param("account") String account);

    @Update("update sms_user_template  set status = #{status} , remark = #{remark},audit_time = now() where id = #{id} and status =1")
    int updateTemplateById(@Param("remark") String remark, @Param("status") Integer status, @Param("id") Integer id);

    @SelectProvider(type = UserTemplateSqlProvider.class, method = "selectUserTemplateListByParams")
    List<SmsUserTemplate> selectUserTemplateListByParams(UserTemplateQueryBean bean);

    class UserTemplateSqlProvider {
        public String selectUserTemplateListByParams(UserTemplateQueryBean bean) {
            return new SQL() {
                {
                    SELECT("*");
                    FROM("sms_user_template s");

                    if (!StringUtils.isEmpty(bean.getAccount())) {
                        WHERE("s.account LIKE concat('%',#{account},'%')");
                    }
                    if (!StringUtils.isEmpty(bean.getTemplate())) {
                        WHERE("s.template LIKE concat('%',#{template},'%')");
                    }
                    if (!StringUtils.isEmpty(bean.getStatus())) {
                        WHERE("s.status = #{status}");
                    }
                    if (bean.getStartTime() != null) {
                        WHERE("s.create_time >= #{startTime}");
                    }
                    if (bean.getEndTime() != null) {
                        WHERE("s.create_time <= date_add(#{endTime}, interval 1 day)");
                    }
                    ORDER_BY("id desc");
                }
            }.toString();
        }
    }
}
